package com.zjlanyun.erp.interior;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.alibaba.fastjson.JSON;
import com.yolanda.nohttp.rest.Response;
import com.zjlanyun.erp.Data;
import com.zjlanyun.erp.R;
import com.zjlanyun.erp.adapter.CommonAdapter;
import com.zjlanyun.erp.config.AppConfig;
import com.zjlanyun.erp.network.WebApi;
import com.zjlanyun.erp.nohttp.HttpListener;
import com.zjlanyun.erp.nohttp.HttpRequest;
import com.zjlanyun.erp.nohttp.SimpleHttpListener;
import com.zjlanyun.erp.view.BaseSwipeBackActivity;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.Bind;

/**
 * @author: shun
 * @date: 2016-09-28 08:54
 * @Desc: 通用列表页面
 */
public class CommonList extends BaseSwipeBackActivity {

    @Bind(R.id.list) ListView mListView;

    private ArrayList<HashMap<String, Object>> mList;
    private String action;
    private Bundle bundle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.common_list);
        initBaseView();
        initView();
    }

    private void initView() {
        bundle = getIntent().getExtras();
        action = bundle.getString("action");
        switch (action) {
            case "dept":
                dept();
                break;
            case "stock":
                stock();
                break;
            case "common":
                common();
                break;
        }
    }

    /**
     * 通用
     */
    private void common() {
        title.setText(bundle.getString("title"));
        String list = bundle.getString("list");
        int[] layout = bundle.getIntArray("layout");
        String[] field = bundle.getStringArray("field");
        mList = new ArrayList<>();
        com.alibaba.fastjson.JSONArray json = JSON.parseArray(list);
        for (int i = 0; i < json.size(); i++) {
            com.alibaba.fastjson.JSONObject item = json.getJSONObject(i);
            HashMap<String, Object> map = new HashMap<>();
            for (int j = 0; j < field.length; j++) {
                map.put(field[j], item.get(field[j]));
            }
            mList.add(map);
        }
        CommonAdapter commonAdapter = new CommonAdapter(mContext, mList, layout, field);
        mListView.setAdapter(commonAdapter);

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = getIntent();
                Bundle bundle = new Bundle();
                bundle.putInt("position", position);
                getIntent().putExtras(bundle);
                setResult(RESULT_OK, intent);
                finish();
            }
        });
    }

    /**
     * 选择部门
     */
    private void dept() {
        title.setText("选择部门");
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = getIntent();
                Bundle bundle = new Bundle();
                bundle.putString("FName", mList.get(position).get("FName").toString());
                bundle.putInt("FItemID", (Integer) mList.get(position).get("FItemID"));
                intent.putExtras(bundle);
                setResult(RESULT_OK, intent);
                finish();
            }
        });

        if (AppConfig.getInstance().getBoolean("isDemo", true)) {
            mList = new ArrayList<>();
            for (int i = 0; i < Data.CPRK_FDeptName.length; i++) {
                HashMap<String, Object> map = new HashMap<>();
                map.put("FName", Data.CPRK_FDeptName[i]);
                map.put("FItemID", Data.FItemID[i]);
                mList.add(map);
            }
            mListView.setAdapter(new SimpleAdapter(mContext, mList, android.R.layout.simple_list_item_1, new String[]{"FName"}, new int[]{android.R.id.text1}));
        } else {
            HashMap<String, Object> param = new HashMap<>();
            param.put("FTableName", "t_Department");
            param.put("FFieldSQL", "FItemID,FNumber,FName");
            param.put("FSCFieldName1", "");
            param.put("FSCValue1", "");
            param.put("FSCFieldName2", "");
            param.put("FSCValue2", "");
            param.put("FOrderByField", "Order By FNumber");
            HttpRequest client = new HttpRequest(mContext, WebApi.production());
            client.setDefault();
            client.getRequest().add("action", "QueryComTable");
            client.getRequest().add("param", JSON.toJSONString(param));
            client.send(new SimpleHttpListener<JSONObject>() {
                @Override
                public void onSucceed(int what, Response<JSONObject> response) {
                    mList = new ArrayList<>();
                    JSONObject data = response.get().optJSONObject("data");
                    JSONArray list = data.optJSONArray("list");
                    for (int i = 0; i < list.length(); i++) {
                        HashMap<String, Object> map = new HashMap<>();
                        JSONObject item = list.optJSONObject(i);
                        map.put("FName", item.optString("FName"));
                        map.put("FItemID", item.optInt("FItemID"));
                        mList.add(map);
                    }
                    mListView.setAdapter(new SimpleAdapter(mContext, mList, android.R.layout.simple_list_item_1, new String[]{"FName"}, new int[]{android.R.id.text1}));
                }
            });
        }
    }

    /**
     * 仓库
     */
    private void stock() {
        title.setText("选择仓库");

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = getIntent();
                Bundle bundle = new Bundle();
                bundle.putString("FName", mList.get(position).get("FName").toString());
                intent.putExtras(bundle);
                setResult(RESULT_OK, intent);
                finish();
            }
        });

        if (AppConfig.getInstance().getBoolean("isDemo", true)) {
            mList = new ArrayList<>();
            for (int i = 0; i < Data.XSCK_FStockName.length; i++) {
                HashMap<String, Object> map = new HashMap<>();
                map.put("FName", Data.XSCK_FStockName[i]);
                mList.add(map);
            }
            mListView.setAdapter(new SimpleAdapter(mContext, mList, android.R.layout.simple_list_item_1, new String[]{"FName"}, new int[]{android.R.id.text1}));
        } else {
            HashMap<String, Object> param = new HashMap<>();
            param.put("FTableName", "t_Stock");
            param.put("FFieldSQL", "FItemID,FNumber,FName");
            param.put("FSCFieldName1", "");
            param.put("FSCValue1", "");
            param.put("FSCFieldName2", "");
            param.put("FSCValue2", "");
            param.put("FOrderByField", "Order By FNumber");
            HttpRequest client = new HttpRequest(mContext, WebApi.production());
            client.setDefault();
            client.getRequest().add("action", "QueryComTable");
            client.getRequest().add("param", JSON.toJSONString(param));
            client.send(new SimpleHttpListener<JSONObject>() {
                @Override
                public void onSucceed(int what, Response<JSONObject> response) {
                    mList = new ArrayList<>();
                    JSONObject data = response.get().optJSONObject("data");
                    JSONArray list = data.optJSONArray("list");
                    for (int i = 0; i < list.length(); i++) {
                        HashMap<String, Object> map = new HashMap<String, Object>();
                        JSONObject item = list.optJSONObject(i);
                        map.put("FName", item.optString("FName"));
                        mList.add(map);
                    }
                    mListView.setAdapter(new SimpleAdapter(mContext, mList, android.R.layout.simple_list_item_1, new String[]{"FName"}, new int[]{android.R.id.text1}));
                }
            });
        }
    }
}
